// JavaScript Document
((function(a){
	//插件本体
	var keycode = function(opts){
		
		//keycode参考信息
		this.keycode = {
			'A':65,'B':66,'C':67,'D':68,'E':69,'F':70,'G':71,'H':72,'I':73,'J':74,'K':75,'L':76,'M':77,'N':78,'O':79,'P':80,'Q':81,'R':82,'S':83,'T':84,'U':85,'V':86,'W':87,'X':88,'Y':89,'Z':90,
			'F1':112,'F2':113,'F3':114,'F4':115,'F5':116,'F6':117,'F7':118,'F8':119,'F9':120,'F10':121,'F11':122,'F12':123}
		
		//evt.preventDefault();
		//evt.stopPropagation();
		//evt.keyCode = 0;
    	//evt.cancelBubble = true;
		//库数据信息
		this.library = {
			//所有的数据的值的信息
			//按钮按下的列表
			keydown:[],
			keyup:[],
			keypress:[],
		};
		
		//监听说有的按钮的一个点击
		this.listen = function(){
			var ths = this;
			a.onkeydown = function(evt){
				var list = ths.library.keydown;
				for(var i = 0;i<list.length;i++){
					if(list[i].check(evt)){
						return list[i].callback(evt);	
					}
				}
			}
			a.onkeyup = function(evt){
				var list = ths.library.keyup;
				for(var i = 0;i<list.length;i++){
					if(list[i].check(evt)){
						return list[i].callback(evt);	
					}
				}
			}
			a.onkeypress = function(evt){
				var list = ths.library.keypress;
				for(var i = 0;i<list.length;i++){
					if(list[i].check(evt)){
						return list[i].callback(evt);	
					}
				}
			}
			//console.log(window.event.keyCode);
		};
		
		//创建一个key的事件
		this.evt = function(evt){
			var ths = this;
			return new function kevent(){
				this.top = ths;
				this.key = evt.key;
				this.keyCode = evt.keyCode
				this.keydown = function(func){
					var _event = new a.KeyboardEvent('keydown',{
						key:this.key,
						keyCode:this.keyCode,
					});
					_event.callback = func;
					_event.check=function(ev){
						if(ev.keyCode==this.keyCode){
							//规则命中
							_event.event = ev;
							return true;
						}else{
							return false;	
						}
					}
					ths.library.keydown.push(_event);	
				},
				//按钮点击按上
				this.keyup = function(func){
					var _event = new a.KeyboardEvent('keyup',{
						key:this.key,
						keyCode:this.keyCode,
					});
					_event.callback = func;
					_event.check=function(ev){
						if(ev.keyCode==this.keyCode){
							//规则命中
							_event.event = ev;
							return true;
						}else{
							return false;	
						}
					}
					ths.library.keyup.push(_event);	
				},
				//按钮长按
				this.keypress = function(func){
					var _event = new a.KeyboardEvent('keypress',{
						key:this.key,
						keyCode:this.keyCode,
					});
					_event.callback = func;
					_event.check=function(ev){
						if(ev.keyCode==this.keyCode){
							//规则命中
							_event.event = ev;
							return true;
						}else{
							return false;	
						}
					}
					ths.library.keypress.push(_event);	
				};
			}
		};
		
		/**f5 = 116*/
		this.searchKey = function(key){
			key = a.kuoqiJs.strtoupper(key);
			var opts = {key:key,keyCode:this.keycode[key]};
			return this.evt(opts);
			
			
			var code = this.keycode[key];
			var evt = new a.KeyboardEvent(type,{
					key:key,
					keyCode:code,
				});
			return this.evt(evt);
		}
		
		this.listen();
		/*a.addEventListener('beforeunload', function(evt){
			console.log(evt);
			alert('1111111');
		})*/
	};
	a.kuoqiJs.plugIn.register('keycode',keycode);
})(this))